package com.yz.Queue;

import java.util.Scanner;

public class ArrayQueue {
    public static void main(String[] args) {
        //测试
        Queue arrayQueue = new Queue(3);
        Scanner sc = new Scanner(System.in);
        char key = ' ';
        boolean a = true;
        while (a) {
            System.out.println("a(add): 为数组添加元素");
            System.out.println("r(remove): 为数组取出元素");
            System.out.println("s(show): 为数组展示所有元素");
            System.out.println("f(first): 为数组展示首数组元素");
            System.out.println("e(exit): 退出！");
            System.out.println("请输入你的指令：");
            key = sc.next().charAt(0);  //在这里接收一个字符
            switch (key) {
                case 'a':
                    System.out.println("请输入一个数：");
                    int i = sc.nextInt();
                    arrayQueue.add(i);
                    break;
                case 'r':
                    try {
                        int res = arrayQueue.remove();
                        System.out.printf("取出的数据是：%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                case 's':
                    arrayQueue.show();
                    break;
                case 'f':
                    try {
                        arrayQueue.First();
                        System.out.printf("队列的头数据是%d\n", arrayQueue.First());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    a = false;
                    break;
            }
        }
        System.out.println("退出成功！");
    }
}
//定义一个队列用数组的形式来模拟
class Queue {
    private int MaxSize;
    private int front;
    private int rear;
    private int[] array;

    public Queue(int Max) {
        //这里传进来的是该队列的长度,并对这些数值进行初始化
        MaxSize = Max;
        front = -1;  //代表队列的其首元素的前一个元素 代表添加的
        rear = -1;   //代表取出的元素下标
        array = new int[MaxSize];
    }

    //1.判断该队列是否为空
    public Boolean isEmtpy() {
        if (front == rear) {
            return true;
        } else {
            return false;
        }
    }

    //2.判断该队列是否满栈
    public Boolean isFull() {
        if (front == MaxSize - 1) {
            return true;
        } else {
            return false;
        }
    }

    //3.为该队列添加元素
    public void add(int num) {
        //添加元素之前判断有没有满
        if (!isFull()) {
            //没有满。可以添加元素
            array[++front] = num;
        } else {
            System.out.println("该队列已经满了，无法添加元素");
        }
    }

    //4.将该队列的元素取出
    public int remove() {
        if (isEmtpy()) {
            throw new RuntimeException("该队列是空的，无法取出元素！");
        } else {
            rear++;
            return array[rear];
        }
    }

    //5.查看该队列的首队列数据
    public int First() {
        if (!isEmtpy()) {
            return array[rear + 1];
        } else {
            throw new RuntimeException("该队列是空的，无法取出元素！");
        }
    }

    //6.显示所有数据
    public void show() {
        if (isEmtpy()) {
            System.out.println("队列是空的，没有数据展示！");
        } else {
//            for (int i : array) {
//                System.out.printf("array[%d] = %d\n", i, array[i]);
//            }
            for (int i = 0; i < array.length; i++) {
                System.out.printf("array[%d] = %d\n", i, array[i]);
            }
        }
    }
}
